package com.mininglamp.utils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.time.DateFormatUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class StringUtils {
	
	public static String generatedId() {
		return UUID.randomUUID().toString();
	}
	
	/*public static String getMapValue(Map<String, String> map,String key){
		if(org.apache.commons.lang3.StringUtils.isBlank(key)) {
			return "";
		}else {
			return map.get(key);
		}
		
	}*/

	/**
	 * 获取文件上传的路径,根据日期生成
	 * @param base
	 * @return
	 */
	public static String getUploadFolder() {
		String pattern = File.separator + "yyyy" + File.separator + "MM" + File.separator + "dd";
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	
	public static String toJson(Map<String,Object> list) {
		ObjectMapper mapper = new ObjectMapper();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(format);
		String json="";
		try {
			json = mapper.writeValueAsString(list);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	public static String toJson(Object object) {
		ObjectMapper mapper = new ObjectMapper();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(format);
		String json="";
		try {
			json = mapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return json;
	}
	
	public static Object toObject(String json) {
		ObjectMapper mapper = new ObjectMapper();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		mapper.setDateFormat(format);
		
		Object obj=null;
		try {
			obj = mapper.readValue(json,Object.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return obj;
	}
	
}
